/**
 * @author 开发请按照此风格编写js
 */
var meterCtrl  = new XFApp.client();

XFApp.client.prototype.meter = {
	urlItems : {
		getMeterDataURL : basePath+"/statistics/selectStat",
		saveArriveNumURL : basePath+"/statistics/saveArriveNum",
	},
	queryColumns : function(){
		var _this = this;
		$.ajax({
		    url:_this.urlItems.getMeterDataURL,
		    data:{date:"2017-01-19"},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(true == data.isSuccess  && data.code==10000){
		    		$("#page_grid").html($("#dataTmp").render(data.result.columns));
		    		var nameArr = new Array();
		    		for (var i = 0 ; i < data.result.pieChart.length; i ++) {
		    			nameArr.push(data.result.pieChart[i].name);
		    		}
		    		_this.initChart(data.result.pieChart,nameArr);
		    		_this.initTitleNum(data.result);
		    	}else{
		    		
		    	}
		    	
		    	
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
		
	},
	initTitleNum : function(data){
		if(data.preSendNum){
			$('#preSendNum').html(data.preSendNum);
		}
		if(data.arriveNum){
			$('#arriveNum').html(data.arriveNum);
		}
		if(data.declaredNum){
			$('#declaredNum').html(data.declaredNum);
		}
		if(data.timeCompair){
			if(data.timeCompair>0){
				$('#upOrDown').html('↓'+data.timeCompair);
			}else{
				$('#upOrDown').html('↓'+Math.abs(data.timeCompair));
			}
			
		}
		if(data.time){
			var times = data.time.split(':');
			if(times.length==3){
				$('#timeHou').html(times[0]);
				$('#timeMin').html(times[1]);
				$('#timeSec').html(times[2]);
			}
			if(times.length==2){
				$('#timeHou').parent().hide();
				$('#timeMin').html(times[0]);
				$('#timeSec').html(times[1]);
			}
			if(times.length==1){
				$('#timeHou').parent().hide();
				$('#timeMin').parent().hide();
				$('#timeSec').html(times[0]);
			}
		}
	},
	initChart : function(data,nameArr){
		var myChart = echarts.init(document.getElementById('pieChart'));
		var option = {
		    title : {
		        text: '当日申报数据分布图',
		        y:'bottom'
		    },
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		        //orient: 'vertical',
		        y : 'top',
		        data: nameArr
		    },
		    series : [
		        {
		            name: '当日申报数据分布图',
		            type: 'pie',
		            radius : '55%',
		            center: ['50%', '60%'],
		            data: data,
		            itemStyle: {
		                emphasis: {
		                    shadowBlur: 10,
		                    shadowOffsetX: 0,
		                    shadowColor: 'rgba(0, 0, 0, 0.5)'
		                }
		            }
		        }
		    ]
		};
		myChart.setOption(option);

	},
	changeArriveNum: function(value){
		var _this = this;
		$('#arriveNum').hide();
		$('#arriveNum_edit').show();
		$('#arriveNum_edit').focus();
		$('#arriveNum_edit').val(value);
		$('#arriveNum_edit').on("blur",function(){
			if($('#arriveNum_edit').val()){
				_this.saveArriveNum(value,$('#arriveNum_edit').val());
			}else{
				_this.saveArriveNum(value,value);
			}
			
		})
	},
	saveArriveNum: function(oldValue,newValue){
		var _this = this;
		$.ajax({
		    url:_this.urlItems.saveArriveNumURL,
		    data:{num:newValue},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(true == data.isSuccess  && data.code==10000){
		    		$('#arriveNum').html(newValue);
		    	}else{
		    		$('#arriveNum').html(oldValue);
		    	}
		    	$('#arriveNum').show();
				$('#arriveNum_edit').hide();
		    },
		    error: function(){
		    	$('#arriveNum').html(oldValue);
		    	$('#arriveNum').show();
				$('#arriveNum_edit').hide();
		    }
		});    
	},
	timeFormat: function(value){
		var s = value.split(":");
		if(s.length>2){
			return s[0]+"小时"+s[1]+"分"+s[2]+"秒";
		}else if(s.length==2){
			return s[0]+"分"+s[1]+"秒";
		}else if(s == 0){
			return 0;
		}else{
			return "统计数据出错";
		}
	},
	event: function(){
		var _this = this;
		$('#arriveNum').on("click",function(){
			_this.changeArriveNum(this.innerHTML);
		})
	}
};
$.views.converters({
	ftime : function(value) {
		return meterCtrl.meter.timeFormat(value) ;
	}
});
/**
 * 初始化
 */
$(function(){
	meterCtrl.meter.queryColumns();
	//meterCtrl.meter.event();
});
